package com.yx.learn.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.yx.learn.domain.Position;
import com.yx.learn.domain.User;
import com.yx.learn.service.PositionService;
import com.yx.learn.service.UserService;

@Controller("positionAction")
@Scope("prototype")
public class PositionAction extends BaseAction implements ModelDriven<Position>{

	private static final long serialVersionUID = 1L;
	private Position position;
	private int page;
	private int rows;
	@Resource
	private PositionService positionService;
	@Resource
	private UserService userService;
	
	public void save(){
		try {
			positionService.savePosition(position);
			this.toResponse("{\"status\":\"ok\"}");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void delete(){
		try {
			if(position.getId()!=1){
				String hql = "From User where positionID="+position.getId();
				List<User> users = userService.findAllUser(hql); 
				//将该职位的用户的职位设置成未分配id为1
				for(User u: users){
					u.getPosition().setId(1);
					userService.updateUser(u);
				}
				positionService.deletePosition(positionService.findById(position.getId()));
				this.toResponse("{\"status\":\"ok\"}");
			}else{
				this.toResponse("{\"status\":\"fail\" , \"message\":\"系统默认未分配职位，不能删除!\"}");
			}			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void update(){
		System.out.println("position.getId()="+position.getId());
		try {
			if(position.getId()!=1){
				positionService.updatePositon(position);
				this.toResponse("{\"status\":\"ok\"}");
			}else{
				this.toResponse("{\"status\":\"fail\" , \"message\":\"系统默认未分配职位，不能更改!\"}");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	
	public void findAllPositions(){
		try {
			String json = positionService.findAllToJson();
			this.toResponse(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}
	
	public void findPositions(){
		try {
			String json = positionService.findPositions(page, rows);
			this.toResponse(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void positionsToCombobox(){
		try {
			String json = positionService.positionsToCombobox();
			this.toResponse(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
	}

	public Position getModel() {
		// TODO Auto-generated method stub
		if(position==null){
			position = new Position();
		}
		return position;
	}
	

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}


}
